package cn.edu.nju.software.netlyric;

import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Scanner;
import java.util.prefs.Preferences;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class NetLyric {
	private URL url;
	private String artist;
	private String title;

	private String mlyric = null;
	
	public NetLyric(String art, String tit){
		artist = art;
		title = tit;

		url = generateURL();
	}
	public NetLyric(String s){

		String url1 = "http://mp3.baidu.com/m?f=ms&tn=baidump3lyric&ct=150994944&lf=2&rn=10&word=";
		String url3 = "&lm=-1";
		try {
			String s1 = URLEncoder.encode(s,"UTF-8");
			url = new URL(url1+s1+url3);
		} catch (Exception e1) {
			e1.printStackTrace();
		}

	}
	
	public void setKey(String s){
		String url1 = "http://mp3.baidu.com/m?f=ms&tn=baidump3lyric&ct=150994944&lf=2&rn=10&word=";
		String url3 = "&lm=-1";
			
		try {
			String s1 = URLEncoder.encode(s,"UTF-8");
			url = new URL(url1+s1+url3);
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	
	}
	
	protected URL generateURL(){
		String url1 = "http://mp3.baidu.com/m?f=ms&tn=baidump3lyric&ct=150994944&lf=2&rn=10&word=";
		String url3 = "&lm=-1";
				
		try {
			String s = URLEncoder.encode(artist+" "+title,"UTF-8");
			url = new URL(url1+s+url3);
		} catch (Exception e) {
			e.printStackTrace();
		}		
		return url;

	}
	private void fetchLyric(){
		StringBuffer sb = new StringBuffer();	
		try{
			Scanner in = new Scanner(url.openConnection().getInputStream());
		for(;in.hasNextLine();){
			sb.append(in.nextLine()+"\n");
		}
		mlyric = sb.toString();
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	private String explainLyric(){
		Pattern regex = Pattern.compile(
				"<div style=\"padding-left:10px;line-height:20px;padding-top:1px\">.*?</div>",Pattern.DOTALL);
		Matcher matcher = regex.matcher(mlyric);
		if(matcher.find()){
			mlyric = mlyric.substring(matcher.start(), matcher.end());			
			mlyric = mlyric.replaceAll("<div style=\"padding-left:10px;line-height:20px;padding-top:1px\">|</div>",
					"");
			mlyric = mlyric.replaceAll("<br>","\n");
			mlyric = mlyric.replaceAll("<font style=color:#e10900>|</font>", "");
			regex = Pattern.compile("\"http://.*?\"");
			matcher = regex.matcher(mlyric);
			if(matcher.find()){
				try {
					url = new URL(mlyric.substring(matcher.start()+1,matcher.end()-1));
					fetchLyric();
					regex = Pattern.compile("<td class=\"p14\">.*?</td>",Pattern.DOTALL);
					matcher = regex.matcher(mlyric);
					if(matcher.find()){
						mlyric = mlyric.substring(matcher.start(),matcher.end());
						mlyric = mlyric.replace("<td class=\"p14\">","");
						mlyric = mlyric.replace("</td>", "");
						mlyric = mlyric.replace("<br>", "\n");
						//mlyric = mlyric.replace("[\\d\\d:\\d\\d.\\d\\d]", "");
					}
				} catch (MalformedURLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}			
			}
			return mlyric;
		}
		else {
			return null;
		}
	}

	public String getLyric(){
		fetchLyric();
		String s = explainLyric();
		return s;
	}
	
}
